/**
 * Created by yangyan on 17/12/15.
 */
var strArray = location.search;
productId = strArray.split("?")[1].split("=")[1];
orderId = strArray.split("?")[2].split("=")[1];
// console.log(strArray)
// console.log(productId);
// console.log("传过来的orderId====="+orderId)

var newTime = timeAll();
function textAjax() {
    var timestamp = 'productId:' + productId +'BM' +'timeStamp:' + newTime + 'BM';
    var signs = md5(timestamp);
    signs = signs.toUpperCase();
    $.ajax({
        type: "get",
        url: webUrl + "orderProcessing/getPhoneInfo",

        data: {
            "productId": productId,
            "sign": signs,
            "timeStamp": newTime
        },
        dataType: "JSON",
        async: false,
        success: function (data) {
            // console.log(data);
            if (data.status == 200) {
                var listAllStr;
                for (var k = 0; k < data.data.length; k++) {
                    var productList = data.data[k];
                    listAllStr = '<div class="item">' +
                        '<span class="type-name">' + productList.categoryName +
                        '</span>';
                    if (k == data.data.length - 1) {
                        listAllStr = listAllStr + '<div>';
                        for (var h = 0; h < productList.productStatusList.length; h++) {
                            var itemList = productList.productStatusList[h];
                            var itemStr = '<input type="checkbox"  value=' + itemList.id +'|'+ '>' + itemList.name + '<br>';
                            listAllStr = listAllStr + itemStr;
                        }
                        listAllStr = listAllStr + '</div>';
                    } else {
                        listAllStr = listAllStr + '<select>';
                        if (productList.isMany=="attr"){
                            for (var h = 0; h < productList.productStatusList.length; h++) {
                                var itemList = productList.productStatusList[h];
                                var itemStr = '<option value=' + itemList.id+',' + '>' + itemList.name +
                                    '</option>';
                                listAllStr = listAllStr + itemStr;
                            }
                        }else if (productList.isMany == "0" ||productList.isMany == "1"){
                            for (var h = 0; h < productList.productStatusList.length; h++) {
                                var itemList = productList.productStatusList[h];
                                var itemStr = '<option value=' + itemList.id +'|'+ '>' + itemList.name +
                                    '</option>';
                                listAllStr = listAllStr + itemStr;
                            }
                        }

                        listAllStr = listAllStr + '</select>';
                    }
                    listAllStr = listAllStr + '</div>';
                    $('#selList').append(listAllStr);
                }
            } else if (data.status == 300) {
                document.location.href = '../html/login.html';
            } else {
                alert(data.msg);
            }
        }
    })
}

var inpArr = document.getElementsByTagName("input");
var optionArray = document.getElementsByTagName("option");

// 生成质检单
function buttonClick() {
    result = "";
    for (var i = 0; i < inpArr.length; i++) {
        if (inpArr[i].checked) {
            result += inpArr[i].value;
        }
    }

    MoreResult = "";
    for (var j = 0; j < optionArray.length; j++) {
        if (optionArray[j].selected) {
            MoreResult += optionArray[j].value;
        }
    }
    // console.log(result);
    // console.log(MoreResult);
    var newAttr=MoreResult+result;
    // console.log(newAttr);
    var strS=newAttr.split(',');
    // 获取逗号之前的值
    var strS22=newAttr.replace(strS[strS.length-1], "");
    strS22=(strS22.substring(strS22.length-1)==',')?strS22.substring(0,strS22.length-1):strS22;
// 取消最后一个逗号
//     console.log(strS22);
    // 获取逗号之后的值
    strS=strS[strS.length-1];
    // 取消最后一个"|"
    strS=(strS.substring(strS.length-1)=='|')?strS.substring(0,strS.length-1):strS;
    // console.log(strS);

    var attrArr=strS22;
    var newArray=strS.split("|");
    // console.log(newArray);

    var problemArr='';
    for (var i=0;i<newArray.length;i++){
        problemArr = problemArr + newArray[i] + ',';
    }
    // console.log("获取到的最终attrArr，传给后台====="+attrArr);
    problemArr=(problemArr.substring(problemArr.length-1)==',')?problemArr.substring(0,problemArr.length-1):problemArr;
    // console.log("获取到的最终problemArr，传给后台====="+problemArr);



    if (result == '') {
        alert('多选为空！！！！！！！！')
    } else if (MoreResult == '') {
        alert('单选为空！！！！！！！！')
    }else {
        var timestamp = "timeStamp:" + newTime + "BM" + "orderId:" + orderId + "BM" + "productId:"+ productId + "BM" + "attrArr:" + attrArr + "BM" + "problemArr:" + problemArr + "BM";
        var signs = md5(timestamp);
        signs = signs.toUpperCase();
        // console.log(signs)
        // console.log(newTime)
        $.ajax({
            type: "get",
            url: webUrl + "orderProcessing/orderNotPass",
            data: {
                "timeStamp": newTime,
                "orderId": orderId,
                "productId": productId,
                "attrArr": attrArr,
                "problemArr": problemArr,
                "sign": signs,
            },
            dataType: "JSON",
            async: false,
            success: function (data) {
                console.log(data);
                if (data.status == 200) {
                    dialog('已生成质检单')
                    setTimeout(window.location.href = '../../html/index.html', 2000);
                } else if (data.status == 300){
                    document.location.href = '../html/login.html';
                } else {
                    alert(data.msg)
                }
            }

        })
    }
}



